<template>
  <div class="app-container customer-div">
    <div style="margin-top: 40px">

      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="品类：">
          <el-input v-model="listQuery.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select
                  clearable
                  v-model="listQuery.kingId"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入关键词"
                  :remote-method="remoteMethod2"
                  :loading="loading2">
            <el-option
                    v-for="item in options"
                    :key="item.kingId"
                    :label="item.goodsSn"
                    :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预警状态：">
          <el-select clearable v-model="listQuery.isStockWarning">
            <el-option :key="0" label="禁用" :value="0"></el-option>
            <el-option :key="1" label="启用" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container" style="margin-top:0">
      <el-table
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%;">
        <el-table-column  label="品类" align="center" prop="goodsName" width="150">
        </el-table-column>
        <el-table-column  label="牌号"  align="center" width="200">
          <template slot-scope="scope">
              {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="最低仓位">
          <template slot-scope="scope">
            {{scope.row.goodsMinStock}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="最高仓位">
          <template slot-scope="scope">
            {{scope.row.goodsMaxStock}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="预警状态">
          <template slot-scope="scope">
            {{scope.row.isStockWarning == 0 ? '禁用' : '启用'}}
          </template>
        </el-table-column>
        <el-table-column  align="center" min-width="150" label="操作">
          <template slot-scope="scope">
              <el-button type="text" @click="showForm(scope.row)">设置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :current-page.sync="listQuery.pageNum"
              :page-size="listQuery.pageSize"
              :page-sizes="[10,15,20]"
              :total="count">
      </el-pagination>
    </div>

    <!--设置-->
    <el-dialog
            :close-on-click-modal="false"
            title="设置预警仓位"
            :visible.sync="dialogVisible"
            width="400px">
      <el-form :model="form" size="mini" label-width="140px" :rules="rules" ref="form">
          <el-form-item label="最低仓位：" prop="addressDetail">
            <el-input-number v-model="form.goodsMinStock" :controls="false" :precision="4" class="inputleft"></el-input-number>
          </el-form-item>
          <el-form-item label="最高仓位：" >
            <el-input-number v-model="form.goodsMaxStock" :controls="false" :precision="4" class="inputleft"></el-input-number>
          </el-form-item>
        <el-form-item label="预警状态：" >
          <el-switch v-model="form.isStockWarning">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading" >确定</el-button>
      </span>
    </el-dialog>


  </div>
</template>
<script>
  import {warehouseStockList,queryGoodsStock,queryGoodsStockSummary,setGoodsStockWarning,  kingGoodsList} from '@/api/partner'
  import {goodsList} from '@/api/company'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    pageNum: 1,
    pageSize: 10,
    kingId:null,
    goodsName:'',
    isStockWarning:'',
  };
  export default {
    name: "index",
    data(){
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        options:[],
        loading2:false,
        btnloading:false,
        dialogVisible:false,
        form:{
          goodsMinStock:'',
          goodsMaxStock:'',
          isStockWarning:'',
        },
        btnLoading:false,
        rules:{
          goodsMinStock:[{ required: true, message: '此项必填', trigger: 'blur' }],
          goodsMaxStock:[{ required: true, message: '此项必填', trigger: 'blur' }],
        }
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),
      exportInventoryQueryReport() {
        if (this.roles.indexOf("exportInventoryQueryReport") != -1) {
          return true
        } else {
          return false
        }
      },
    },
    created() {
      this.getList();
    },
    filters: {
      formatCreateTime(time) {
        if(time){
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }else{
          return '--'
        }
      },
    },
    methods:{
      //显示设置弹窗
      showForm(item){
        this.dialogVisible = true;
        this.form.kingId = item.kingId;
        this.form.goodsMinStock = item.goodsMinStock;
        this.form.goodsMaxStock = item.goodsMaxStock;
        this.form.isStockWarning = item.isStockWarning == 1 ? true :false;
      },

      //关闭设置弹窗
      handleClose(){
        this.dialogVisible = false;
        this.btnLoading = false;
        this.form.kingId = '';
        this.form.goodsMinStock = '';
        this.form.goodsMaxStock = '';
        this.form.isStockWarning = '';
      },

      //确认设置
      handleConfirm(){
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.btnLoading = true;
            let data = JSON.parse(JSON.stringify(this.form));
            data.isStockWarning = data.isStockWarning ? 1 :0;
            setGoodsStockWarning(this.form).then((res)=>{
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.btnLoading = false;
              this.handleClose();
              this.getList();
            }).catch(()=>{
              this.btnLoading = false;
            })
          }
        })

      },

      //获取商品列表
      remoteMethod2(query) {
        if (query !== '') {
          this.loading2 = true;
          goodsList({keywords:query}).then((res)=>{
            this.options = res.list;
            this.loading2 = false;
          }).catch(()=>{
            this.loading2 = false;
          });
        } else {
          this.options = [];
        }
      },


      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取列表
      getList() {
        this.listLoading = true;
        kingGoodsList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

    },
  }
</script>

<style scoped lang="scss">

  .upload-div{
    text-align: center;
    position:relative;
    #upload{

      opacity: 0;
      position: absolute;
      z-index: 1;
      width:154px;
      height:40px
    }
  }
</style>
